package com.heima.leadnews.publish.listener;

import com.heima.feign.wemedia.WeMediaFeign;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojo.WmNews;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class PublishNewsListener {

    @Autowired
    private WeMediaFeign weMediaFeign;

    /**
     * 重复消费，文章被发布二次
     * 状态为8的文章，自媒体人可以修改，修改后会进行重新审核。
     * 文章审核未通过，状态修改为 3
     * 文章审核通过，立即发布，修改为 9
     * @param message
     */
    @RabbitListener(queues = "dl.queue")
    public void receiveMessage(Message message) {
        byte[] body = message.getBody();
        String newsId = new String(body);
        //1.远程调用wemedia微服务，根据自媒体文章id查询自媒体文章
        ResponseResult<WmNews> responseResult = weMediaFeign.getNewsId(Integer.valueOf(newsId));
        if (responseResult.getCode() == AppHttpCodeEnum.SUCCESS.getCode()) {
            //2.远程调用wedia微服务，发布文章，
            WmNews wmNews = responseResult.getData();
            //文章审核通过后，可以被修改
            if (wmNews.getStatus() == WmNews.Status.APPROVED && wmNews.getPublishTime().before(new Date())){
                weMediaFeign.publish(wmNews);
            }
        }
    }
}
